﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CodeTunnel.MvcUtilities.ExtensionMethods
{
    /// <summary>
    /// Extension methods for string objects.
    /// </summary>
    public static class StringExtensions
    {
        /// <summary>
        /// Truncates string at index of space closest to maxLength.
        /// </summary>
        /// <param name="text">String value to be truncated.</param>
        /// <param name="maxLength">The maximum length of the truncated string.</param>
        /// <returns>string</returns>
        public static string TruncateOnSpace(this string text, int maxLength)
        {
            if (text.Length >= maxLength)
            {
                text = text.Substring(0, maxLength);
                if (text.Contains(' '))
                    text = text.Substring(0, text.LastIndexOf(' '));
                text += "...";
            }
            return text;
        }
    }
}
